package com.ronglian.iot.config;

import com.ronglian.iot.common.msg.ObjectRestResponse;
import org.apache.log4j.Logger;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: @Vaild注解抛出异常
 * @packe: com.joy.ins.config
 * @author: cao taibai
 * @date: 2021/1/28 16:42
 */

@ControllerAdvice
public class VaildExceptionConfig {

    private static final Logger logger = Logger.getLogger(VaildExceptionConfig.class);

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ObjectRestResponse throwCustomException(MethodArgumentNotValidException methodArgumentNotValidException) {

        logger.error("[ @Vaild异常捕获 ] " + methodArgumentNotValidException.getMessage());

        ObjectRestResponse responseBean = new ObjectRestResponse();
        responseBean.setStatus(500);
        responseBean.setRel(false);
        responseBean.setMessage(methodArgumentNotValidException.getBindingResult().getFieldError().getDefaultMessage());
        return responseBean;
    }
}
